import { login, logout, getUserInfo } from '@/api/user'
import { setToken, getToken, removeToken, setTagNavListInLocalstorage } from '@/libs/util'

export default {
  state: {
    username: '',
    userId: '',
    avatorImgPath: '',
    token: getToken(),
    access: [],
    name: '',
    introduction: '',
    roles: [],
    menus: undefined,
    elements: undefined,
    permissionMenus: undefined
  },
  mutations: {
    setUserId (state, id) {
      state.userId = id
    },
    setUsername (state, name) {
      state.username = name
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ACCESS (state, access) {
      state.access = access
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_TOKEN (state, token) {
      state.token = token
      setToken(token)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_ELEMENTS: (state, elements) => {
      state.elements = elements
    },
    SET_PERMISSION_MENUS: (state, permissionMenus) => {
      state.permissionMenus = permissionMenus
    },
    SET_AVATAR: (state, avatar) => {
      state.avatorImgPath = avatar
    }
  },
  actions: {
    // 登录
    handleLogin ({ commit }, { username, password }) {
      username = username.trim()
      return new Promise((resolve, reject) => {
        login({
          username,
          password
        }).then(res => {
          const data = res
          const token = 'Bearer ' + data.access_token
          commit('SET_TOKEN', token)
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        const token = state.token
        commit('SET_TOKEN', '')
        logout(token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_MENUS', undefined)
          commit('SET_ELEMENTS', undefined)
          commit('SET_PERMISSION_MENUS', undefined)
          commit('SET_ACCESS', [])
          setTagNavListInLocalstorage([])
          removeToken()
          resolve()
        }).catch(err => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_MENUS', undefined)
          commit('SET_ELEMENTS', undefined)
          commit('SET_PERMISSION_MENUS', undefined)
          commit('SET_ACCESS', [])
          setTagNavListInLocalstorage([])
          reject(err)
        })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        // commit('setToken', '')
        // commit('setAccess', [])
        // resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(res => {
          const data = res
          commit('SET_ROLES', 'admin')
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', 'https://avatars0.githubusercontent.com/u/20942571?s=460&v=4')
          commit('SET_INTRODUCTION', data.description)
          const menus = {}
          const access = []
          for (let i = 0; i < data.menus.length; i++) {
            menus[data.menus[i].code] = true
            access.push(data.menus[i].code)
          }
          commit('SET_MENUS', menus)
          commit('SET_ACCESS', access)
          const elements = {}
          for (let i = 0; i < data.elements.length; i++) {
            elements[data.elements[i].code] = true
          }
          commit('SET_ELEMENTS', elements)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
